package actionHandlers;

import java.awt.Point;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import cuShapes.PQShape;
import cuShapes.ShapeList;
import palette.CanvasManager;
import palette.CanvasWindow;
import palette.DrawingWindow;
import java.io.Serializable;


public class PropertiesTableListener implements TableModelListener, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1881103730498772484L;
	private JTable table;
	public PropertiesTableListener(JTable t) {
		table = t;
	}
	
	@Override
	public void tableChanged(TableModelEvent arg0) {
		//System.out.println("thing fired");
		ShapeList slist = ((CanvasWindow)CanvasManager.getInst().getSelectedFrame()).getShapeList();
		PQShape currShape = slist.getCurrentShape();
		Point adjusted;
		
		// check to see what kind of shape so that we know what the table model looks like
		switch(arg0.getFirstRow()) {
		case 0:
			//System.out.println("Name was changed.");
			currShape.setName((String) table.getModel().getValueAt(0, 1));
			slist.update();
			break;
		case 1:
			//System.out.println("X was changed.");
			currShape.setWorkingRect(currShape.getCenterRect());
			adjusted = toCenter(new Point((int) Float.parseFloat((String) table.getModel().getValueAt(1, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(2, 1))), currShape);
			currShape.moveWorkingRect(adjusted);
			slist.update();
			break;
		case 2:
			//System.out.println("Y was changed.");
			currShape.setWorkingRect(currShape.getCenterRect());
			adjusted = toCenter(new Point((int) Float.parseFloat((String) table.getModel().getValueAt(1, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(2, 1))), currShape);
			currShape.moveWorkingRect(adjusted);
			slist.update();
			break;
		case 3:
			//System.out.println("Width was changed.");
			currShape.setWorkingRect(currShape.getResizeRect());
			adjusted = toResize(new Point((int) Float.parseFloat((String) table.getModel().getValueAt(3, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(4, 1))), currShape);
			currShape.moveWorkingRect(adjusted);
			((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();			
			break;
		case 4:
			//System.out.println("Height was changed.");
			currShape.setWorkingRect(currShape.getResizeRect());
			adjusted = toResize(new Point((int) Float.parseFloat((String) table.getModel().getValueAt(3, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(4, 1))), currShape);
			currShape.moveWorkingRect(adjusted);
			((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();
			break;
		case 5:
			//System.out.println("Color was changed.");
			//currShape.setColor((String) table.getModel().getValueAt(5, 1));
			((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();				
			break;
		default:
			break;
		}	
		DrawingWindow currWin = ((DrawingWindow)CanvasManager.getInst().getSelectedFrame());
		try{
			currWin.getUndoStack().push(currWin.getShapeList().getDeepCopy());
		}
		catch(Exception ex){
			System.out.println("Error in deep copy");
		}
		
	}

	public Point toCenter(Point p, PQShape s) {
		Point newPoint = new Point(p.x+(s.getWidth()/2), p.y+(s.getHeight()/2));
		return newPoint;
	}
	
	public Point toResize(Point p, PQShape s) {
		Point newPoint = new Point(s.getStart().x+p.x, s.getStart().y+p.y);
		return newPoint;
	}
	
    
}
